/*******************************************************************************
   \ingroup   liblog
   \file      FileLog.cpp
   \brief     This is a file log device implementation.
   \author    Marco Siciliano

   LibLog implementation.
 
   A library to log into several devices.

   This library has been written 2002 by 
   Marco Siciliano [marco@siciliano.de] and is freeware.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   1. Redistributions of source code must retain the above freeware
   notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above freeware
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   SUCH DAMAGE.

 

*******************************************************************************/ 
#include "FileLog.hpp"

#include <fstream>
#include <stdlib.h>
#include <time.h>

CFileLog::CFileLog(const std::string &Prefix,
           bool bAppendTimestampToLogfileName) :
    CBaseLog("FileLog")
{
    std::cout << "CFileLog::CFileLog:" << std::endl;

    std::string Filename(Prefix);

    if(bAppendTimestampToLogfileName)
    {       
        struct tm *p;
        struct tm tmbuf;
        time_t  Today;

        if(time(&Today) == (time_t)-1)
        {
            std::cerr << "CFileLog::getDateAndTime: "
            "system call time() failed" << std::endl;
            exit(1);
        }

        if((p = localtime_r(&Today, &tmbuf)) == NULL)
        {
            std::cerr << "CFileLog::getDateAndTime: "
            "system call localtime_r() failed" << std::endl;
            exit(1);
        }
        char filename[24] = {0};
        sprintf(filename, "%02d%02d%4d-%02d%02d%02d",  
                p->tm_mday, p->tm_mon + 1,1900+p->tm_year,
                p->tm_hour, p->tm_min, p->tm_sec);

        Filename.append(filename);
    }

    Filename.append(".log");

    m_pFile = new std::fstream(Filename.c_str(), std::ios::out);
    std::cout << "CFileLog::CFileLog: Logfile: " << 
        Filename << " should now be openend" << std::endl;
}

CFileLog::~CFileLog()
{
    m_pFile->close();
}



void CFileLog::logDebug  (const std::string &eventMessage)
{
    *m_pFile << eventMessage;
}

void CFileLog::logMessage(const std::string &eventMessage)
{
    *m_pFile << eventMessage;
}

void CFileLog::logError  (const std::string &eventMessage)
{
    *m_pFile << eventMessage;
}

void CFileLog::logFatal  (const std::string &eventMessage)
{
    *m_pFile << eventMessage;
}

